<script setup lang="ts">
import { v4 as uuid4 } from 'uuid' // 提前安装好uuid模块， pnpm install uuid --save
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import type { ILoginData } from '@/api/user/types'
import { useUserStore } from '@/stores/modules/user'

const userStore = useUserStore()
const loginData = reactive<ILoginData>({
  username: 'admin',
  password: 'atguigu123',
  code: import.meta.env.VITE_API_CODE
})

const formRef = ref<FormInstance>()
// const image_code_id = ref('')
// const image_code_url = ref('')
// const generate_image_code = () => {
//   //  1. 先产生一个uuid
//   image_code_id.value = uuid4() // 利用uuid模块的方法生成一个uuid
//   // 2. 拼接一个图片的地址
//   image_code_url.value = import.meta.env.VITE_SERVE + import.meta.env.VITE_APP_BASE_API + '/user/image_code/' + image_code_id.value + '/'
// }
// onMounted(() => {
//   generate_image_code()
// })
const rules = reactive<FormRules<ILoginData>>({
  username: [
    { required: true, message: '用户名不能空', trigger: 'blur' },
    { min: 3, max: 10, message: '用户名长度需在3-10位之间', trigger: 'blur' }
  ],
  password: [{ required: true, message: '必须输入密码', trigger: 'blur' }],
  code: [{ required: true, message: '必须输入4位验证码', trigger: 'blur' }]
})

const loading = ref(false)

// 登录
const handlerLogin = async () => {
  // 校验表单数据
  await formRef.value?.validate()
  loading.value = true
  userStore.userLogin(loginData).finally(() => (loading.value = false))
}
</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form ref="formRef" label-width="80px" size="large" class="login_form" :model="loginData" :rules="rules">
          <h1>Hello</h1>
          <h2>欢迎来到长江路社区卫生服务中心</h2>
          <el-form-item label="" prop="username">
            <el-input v-model="loginData.username" placeholder="请输入用户名" clearable :prefix-icon="User" />
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input
              v-model="loginData.password"
              prop="password"
              type="password"
              placeholder="请输入密码"
              clearable
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          <el-form-item label="" prop="code">
            <el-row>
              <el-col :span="6"> <el-input v-model="loginData.code" /> </el-col>
              <el-col :span="6"> <div class="code_txt">验证码</div></el-col>
              <el-col :span="12">
                <!-- <img :src="image_code_url" alt="图形验证码" class="pic_code" @click="generate_image_code" /> -->
                <span class="code_txt">点击更新</span>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="handlerLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 30px;
    h1 {
      color: #fff;
      font-size: 40px;
    }
    h2 {
      color: #fff;
      font-size: 20px;
      margin: 20px 0;
    }
    .code_txt {
      color: white;
      font-size: 16px;
      margin-left: 10px;
    }
    .pic_code {
      height: 40px;
      // margin-left: 20px;
    }
    .el-button {
      width: 100%;
      // margin-top: 30px;
    }
  }
}
</style>
